package org.budo.weixin.work.api.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@Getter
@Setter
@NoArgsConstructor
public class ApprovalDetailSpRecord implements SpRecord, Serializable {
    private static final long serialVersionUID = -8678760246955074207L;

    private JSONObject json;

    private Integer index;

    public ApprovalDetailSpRecord(JSONObject json) {
        this.setJson(json);
    }

    @Override
    public List<SpRecordDetail> getDetails() {
        if (null == this.getJson()) {
            log.error("#36 getDetails null, this=" + this);
            return null;
        }

        JSONArray details = this.getJson().getJSONArray("details");
        if (null == details) {
            log.error("#4336 getDetails null, this=" + this);
            return null;
        }

        List<SpRecordDetail> list = new ArrayList<SpRecordDetail>();

        for (Object detail : details) {
            ApprovalDetailSpRecordDetail spRecordDetail = new ApprovalDetailSpRecordDetail(this.getIndex(), (JSONObject) detail);
            list.add(spRecordDetail);
        }

        return list;
    }

    @Override
    public String toString() {
        if (null == this.getDetails() || this.getDetails().isEmpty()) {
            return super.toString() + ", json=" + this.getJson() + ", index=" + this.getIndex();
        }

        return this.getDetails() + "";
    }
}